#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <algorithm>
using namespace std;

const int N = 1e5 + 10;
int n, l[N], r[N];

int dfs(int root)
{
	if (root == 0) return 0;
	return max(dfs(l[root]), dfs(r[root])) + 1;
}

int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> l[i] >> r[i];
	}
	cout<<dfs(1);


	return 0;
}